package com.mediademo;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private static final int RESULT_CAPTURE_IMAGE = 1;// 照相的requestCode
	private static final int REQUEST_CODE_TAKE_VIDEO = 2;// 摄像的照相的requestCode
	private static final int RESULT_CAPTURE_RECORDER_SOUND = 3;// 录音的requestCode
	private static final int RESULT_CAPTURE_SYSRECORDER_SOUND = 4;// 录音的requestCode
	private final Activity activity = this;

	private Button btn_shot;
	private Button btn_video;
	private Button btn_recorder;
	private Button btn_sysRecorder;

	private String strImgPath = "";// 照片文件绝对路径
	private String strVideoPath = "";// 视频文件的绝对路径
	private String strRecorderPath = "";// 录音文件的绝对路径
	private String strRecorderpath_sys = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_main);

		btn_shot = (Button) findViewById(R.id.btn_shot);
		btn_video = (Button) findViewById(R.id.btn_video);
		btn_recorder = (Button) findViewById(R.id.btn_recorder);
		btn_sysRecorder = (Button) findViewById(R.id.btn_sysRecorder);
		
		btn_shot.setOnClickListener(this);
		btn_video.setOnClickListener(this);
		btn_recorder.setOnClickListener(this);
		btn_sysRecorder.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_shot:
			cameraMethod();
			break;

		case R.id.btn_video:
			videoMethod();
			break;

		case R.id.btn_recorder:
			soundRecorderMethod();
			break;
			
		case R.id.btn_sysRecorder:
			soundSysRecorderMethod();
			break;

		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case RESULT_CAPTURE_IMAGE:
			if (resultCode == RESULT_OK) {
				Toast.makeText(this, strImgPath, Toast.LENGTH_SHORT).show();
			}
			break;

		case REQUEST_CODE_TAKE_VIDEO:
			if (resultCode == RESULT_OK) {
				Uri uriVideo = data.getData();
				Cursor cursor = this.getContentResolver().query(uriVideo, null,
						null, null, null);
				if (cursor.moveToNext()) {
					/* _data：文件的绝对路径 ，_display_name：文件名 */
					strVideoPath = cursor.getString(cursor
							.getColumnIndex("_data"));
					Toast.makeText(this, strVideoPath, Toast.LENGTH_SHORT)
							.show();
				}
			}
			break;

		case RESULT_CAPTURE_RECORDER_SOUND:
			if (resultCode == RESULT_OK) {
				Uri uriRecorder = data.getData();
				Cursor cursor = this.getContentResolver().query(uriRecorder,
						null, null, null, null);
				if (cursor.moveToNext()) {
					/* _data：文件的绝对路径 ，_display_name：文件名 */
					strRecorderPath = cursor.getString(cursor
							.getColumnIndex("_data"));
					Toast.makeText(this, strRecorderPath, Toast.LENGTH_SHORT)
							.show();
				}
			}
			break;
			
		case RESULT_CAPTURE_SYSRECORDER_SOUND:
			// 这里我们就可以获取到刚刚录制的音频的Uri，可以进行播放等操作，这里显示返回的Uri
			if (resultCode == RESULT_OK) {
				Uri audioPath = data.getData();
				Toast.makeText(this, audioPath.toString(), Toast.LENGTH_LONG).show();
			}
			break;

		default:
			break;
		}

	}

	/**
	 * 照相
	 */
	private void cameraMethod() {
		Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		strImgPath = Environment.getExternalStorageDirectory().toString()
				+ "/CONSDCGMPIC/";// 存放照片的文件夹
		String fileName = new SimpleDateFormat("yyyyMMddHHmmss")
				.format(new Date()) + ".jpg";// 照片命名
		File out = new File(strImgPath);
		if (!out.exists()) {
			out.mkdirs();
		}
		out = new File(strImgPath, fileName);
		strImgPath = strImgPath + fileName;// 该照片的绝对路径
		Uri uri = Uri.fromFile(out);
		imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
		imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
		startActivityForResult(imageCaptureIntent, RESULT_CAPTURE_IMAGE);
	}

	/**
	 * 录像
	 */
	private void videoMethod() {
		Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
		startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO);
	}

	/**
	 * 录音
	 */
	private void soundRecorderMethod() {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType("audio/amr");
		startActivityForResult(intent, RESULT_CAPTURE_RECORDER_SOUND);
	}
	
	private void soundSysRecorderMethod(){
		Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); 
		startActivityForResult(intent, RESULT_CAPTURE_SYSRECORDER_SOUND); 
	}

}
